define(['datePlugin'], function(datePlugin) {

	

	//本页对象
	var accounts = {
		datePlugin: datePlugin, //日期插件
		initialize: function() {
			accounts.onShow();
			accounts.bindEvents();
		},
		onShow: function() {
			accounts.datePlugin.initialize();
			accounts.create_accountsLis();
			accounts.get_dop();      //获取存款
			accounts.get_goal();
			
		},
		bindEvents: function() {
			$('#YYYY').on('change',function(){
				accounts.create_accountsLis();
			});
			$("#MM").on('change',function(){
				accounts.create_accountsLis();
			});
			$("#DD").on('change',function(){
				accounts.get_Day();
			});
			
			//支出增加条目
			$("#spending_btn").on('click', function(e) {
				accounts.push_accountsLis(e);
			});
			
			//收入增加条目
			$('#income_btn').on('click',function(e){
				accounts.push_accountsLis(e);
			});
			
			//修改存款
			$('#modify_dop_btn').on('click',function(){
				accounts.send_dop();
			});
			
			//发工资了
			$("#get_salary_btn").on('click',function(){
				accounts.add_salary();
			});
			
			//修改目标
			$('#modify_goal_btn').on('click',function(){
				accounts.modify_goal();
			});
		},
		
		//生成条目li
		create_accountsLis: function() {
			var type = "create_accountsLis";
			var user_id = localStorage.getItem("blog_user_id");
			var date_year = $("#YYYY").val();
			var date_month = $("#MM").val();
			
			
        	$.ajax({
        		type:"get",
        		url:"http://127.0.0.1:8686",
        		async:true,
        		data:{"user_id":user_id,"date_month":date_month,"date_year":date_year,"type":type},
        		dataType:'text',
        		success:function(data){
        			var result = JSON.parse(data);        			
        			accounts.accountsLis_month = result;
        			accounts.get_Day();   //要执行一次
        			
        			
        		},
        		error:function(err){
        			console.log(err);
        		}
        	});
		},
		
		//获取 日
		get_Day:function(){
			var result = accounts.accountsLis_month;         //获取日的数据对象
			var accountsLis_data = [];
			
			var date_day = $("#DD").val();
			
			var month_amount = 0;
			var day_amount = 0;
			
			for(var i=0;i<result.length;i++){
				var item = result[i];
				var result_day = parseInt(item.date.split('T')[0].split('-')[2])+1;
				
				month_amount+=item.amount;   //计算月总花
				
				if(date_day==result_day){
					accountsLis_data.push(item);
					day_amount += item.amount;        //计算日总花
				}
			}
			console.log(accountsLis_data);
			var accountsLis_tpl_html = $("#show_item_template").html();
        	var accountsLis_fn = _.template(accountsLis_tpl_html);
        	var accountsLis_tpl = accountsLis_fn({
        		data:accountsLis_data
        	});
        	$(".show_item").html(accountsLis_tpl);
        	console.log(day_amount+','+month_amount);
        	
        	//month_amount or day_amount > 0 代表这个月是收入状态的
        	if(day_amount>0){
        		$('.day_amount').html("0,(+" + day_amount + ")");
        	}else{
        		$(".day_amount").html(-(day_amount));	
        	}
        	
        	if(month_amount>0){
        		$('.month_amount').html("0,(+" + month_amount + ")");
        	}else{
        	    $(".month_amount").html(-(month_amount));	
        	}
        	
        	
        	
		},
		
		//发送条目
		push_accountsLis: function(e) {
			var user_id = localStorage.getItem('blog_user_id');
			var bslDate = new Date();
			//var myDate = bslDate.format('yyyy-MM-dd');
			console.log($('#YYYY').val()+'-'+$('#MM').val()+'-'+$('#DD').val());
			var myDate = $('#YYYY').val()+'-'+$('#MM').val()+'-'+$('#DD').val();
			var matters = $("#matters").val();
			
			console.log($(e.target).attr('data-value'));
			var spend_or_income = $(e.target).attr('data-value');
			if(spend_or_income == 'spending'){
				var amount = -($("#amount").val());
				console.log(amount);
			}else{           // spend_or_income == 'income'
				var amount = $("#amount").val();
			}
			
			var type = 'accounts_push_accountsLis';
			console.log(user_id + ',' + myDate + ',' + ',' + matters + ',' + amount);
			$.ajax({
				type: "get",
				url: "http://127.0.0.1:8686",
				async: true,
				data: {
					"user_id": user_id,
					"myDate": myDate,
					"matters": matters,
					"amount": amount,
					"type":type
				},
				dataText: 'text',
				success: function(data) {
                    console.log(data);
                    accounts.create_accountsLis();
                    $("#matters").val("");
                    $("#amount").val("");
                    
                    var dop = parseInt($(".dop_num").html()) + parseInt(amount);
                    console.log(dop);
                    accounts.send_dop_ajax(dop);
                    
				},
				error: function(err) {
                    console.log(err);
				}
			});

		},
		
		//发送存款
		send_dop:function(){
			var dop = $("#modify_dop").val();
			accounts.send_dop_ajax(dop);
		},
		
		//发送存款ajax
		send_dop_ajax:function(dop){
			var user_id = localStorage.getItem('blog_user_id');
			var type = "send_dop";
			$.ajax({
				type:"get",
				url:"http://127.0.0.1:8686",
				async:true,
				data:{"dop":dop,"user_id":user_id,"type":type},
				dataType:'text',
				success:function(data){
					console.log(data);
					$("#modify_dop").val("");
					accounts.get_dop();
				},
				error:function(err){
					console.log(err);
				}
			});
		},
		
		//获得存款
		get_dop:function(){
			var user_id = localStorage.getItem('blog_user_id');
			var type = "get_dop";
			$.ajax({
				type:"get",
				url:"http://127.0.0.1:8686",
				async:true,
				data:{"user_id":user_id,"type":type},
				dataType:'text',
				success:function(data){
					console.log(data);
					var result = JSON.parse(data);
					$(".dop_num").html(result[0].deposit);
				},
				error:function(err){
					console.log(err);
				}
			});
		},
		
		//发工资了
		add_salary:function(){
			var dop = parseInt($(".dop_num").html());
			var salary = parseInt($("#get_salary").val());
			var dop = dop + salary;
			console.log(dop);
			accounts.send_dop_ajax(dop);
			$("#get_salary").val("");
		},
		
		//修改目标
		modify_goal:function(){
			var user_id = localStorage.getItem('blog_user_id');
			var goal = $("#modify_goal").val();
			var type = 'modify_goal';
			$.ajax({
				type:"get",
				url:"http://127.0.0.1:8686",
				async:true,
				data:{"user_id":user_id,"goal":goal,"type":type},
				dataType:'text',
				success:function(data){
					accounts.get_goal();
					$("#modify_goal").val("");
				},
				error:function(err){
					console.log(err);
				}
			});
		},
		
		//获取存款:
		get_goal:function(){
			var user_id = localStorage.getItem('blog_user_id');
			var type = "get_goal";
			$.ajax({
				type:"get",
				url:"http://127.0.0.1:8686",
				async:true,
				data:{"user_id":user_id,"type":type},
				dataType:'text',
				success:function(data){
					console.log(typeof data);
					var result = JSON.parse(data);
					console.log(result[0].goal);
					$(".goal_num").html(result[0].goal);
				},
				error:function(err){
					console.log(err);
				}
			});
		}
		
	}

	return accounts;
})